<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket客户端</title>
</head>
<body>
    <script type="text/javascript">
        var socket;
        if(window.WebSocket){
            socket = new WebSocket("ws://localhost:8001/ws");
            socket.onmessage = function (ev) {
                var ta = document.getElementById("responseText");
                ta.value = ta.value +"\n"+ev.data;
            }
            socket.onopen = function (ev) {
                var ta = document.getElementById("responseText");
                ta.value = "连接开启!!";
            }
            socket.onclose = function (ev) {
                var ta = document.getElementById("responseText");
                ta.value = ta.value +"\n"+"连接关闭!!";
            }

        }else{
            alert("浏览器不支持webSocket")
        }

        function send(messgae) {
            if(!window.WebSocket){
                return;
            }
            if(socket.readyState == WebSocket.OPEN){
                socket.send(messgae);
            }else {
                alert("连接尚未开启!!");
            }

        }




    </script>
    <form onsubmit="return false">
        <textarea name="message" style="width: 400px ; height: 200px "></textarea>
        <input type="button" value="发送数据" onclick="send(this.form.message.value)">
        <H3>服务端数据</H3>
        <textarea id="responseText" style="width: 400px ; height: 200px "></textarea>
        <input type="button" value="清空数据" onclick="javascript: document.getElementById('responseText').value=''">
    </form>

</body>
</html>